/* 

Replication syntax BJPolS article
"Intergenerational social mobility, political socialization, and support for the left under post-industrial realignment"
Dr. Macarena Ares, University of Barcelona
Dr. Mathilde M. van Ditmars, University of Lucerne
March 2022 (Stata version 16)

File 3/4. SHP Data preparation 
*/

*Swiss Household Panel, 1999-2019
*Data version used: W1-21; DOI: 10.23662/FORS-DS-932-6
*see https://www.swissubase.ch/en/catalogue/studies/6097/17411/datasets/932/2032/overview

*DATASET WORKING FILE CREATION*

global datadest "DEFINE"
set more off

cd  "DEFINE"


*adding yearly variables for all the waves, long data file creation:

local year = 1999 // this is a macro, it has the value 1999
  
foreach y in 99 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 {
use idpers status?? idspou?? idhous?? plingu?? noga2m?? p??w31 is4maj?? xis4ma?? x??w03 ///
educat?? civsta?? age?? relarp?? occupa?? nat_1_?? nat_2_?? nat_3_?? p??p10 p??p19 p??p01 tr1maj?? ///
wstat?? ownkid?? gldmaj?? p??w87 p??w90 i??ptotg i??ptotn ///
using shp`y'_p_user if status`y'==0, clear
renvars status?? idspou?? idhous?? plingu?? noga2m?? is4maj?? xis4ma?? nat_1_?? nat_2_?? nat_3_?? ///
educat?? civsta?? age?? relarp?? occupa?? tr1maj?? wstat?? ownkid?? gldmaj??, postsub(`y') 
renvars p`y'* , presub(p`y' p)
renvars x`y'* , presub(x`y' x)
renvars i`y'* , presub(i`y' i)
gen year=`year' // replaces `year' with the value of the macro 
save $datadest/tempp`y', replace
      local year = `year' + 1 // changes the value of the macro
   }
  
foreach y in 99 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 {
use idhous?? i??dispy region?? com2_?? using shp`y'_h_user, clear
renvars i`y'* , presub(i`y' i)
renvars idhous?? region?? com2_??, postsub(`y') 
save $datadest/temph`y', replace
   } 
   
foreach y in 99 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19  {
use $datadest/tempp`y'
merge m:1 idhous using $datadest/temph`y', nogen
keep if status==0
save $datadest/tempp`y', replace
}
  
foreach y in 99 00 01 02 03 04 05 06 07 08 09 11 14 17 {
use idpers p??p13 p??p15 p??p17 using shp`y'_p_user, clear
renvars p`y'* , presub(p`y' p)
save $datadest/temp_prefs`y', replace
   } 
 
 
foreach y in 99 00 01 02 03 04 05 06 07 08 09 11 14 17 {
use $datadest/tempp`y'
merge 1:1 idpers using $datadest/temp_prefs`y', nogen
keep if status==0
save $datadest/tempp`y', replace
}

foreach y in 00 01 02 03 04 05 06 07 08 09 11 14 17 {
use idpers p??p20 p??p21 p??p22 using shp`y'_p_user, clear
renvars p`y'* , presub(p`y' p)
save $datadest/temp_gender`y', replace
   } 
   
foreach y in 00 01 02 03 04 05 06 07 08 09 11 14 17 {
use $datadest/tempp`y'
merge 1:1 idpers using $datadest/temp_gender`y', nogen
keep if status==0
save $datadest/tempp`y', replace
}

foreach y in 99 00 01 02 03 04 05 06 07 08 09 12 15 18 {
use idpers p??r01 p??r04 using shp`y'_p_user, clear
renvars p`y'* , presub(p`y' p)
save $datadest/temp_reli`y', replace
   }
   
foreach y in 99 00 01 02 03 04 05 06 07 08 09 12 15 18 {
use $datadest/tempp`y'
merge 1:1 idpers using $datadest/temp_reli`y', nogen
keep if status==0
save $datadest/tempp`y', replace
}

foreach y in 11 14 17 {
use idpers p??p50 p??p51 p??p52 p??p53 p??p54 p??p55 p??p56 p??p57 p??p58 p??p59 p??p60 ///
p??p61 p??p62 p??p63 p??p66 p??p67 p??p68 p??p75 using shp`y'_p_user, clear
renvars p`y'* , presub(p`y' p)
save $datadest/temp_polmodule`y', replace
   }
   
foreach y in 11 14 17 {
use $datadest/tempp`y'
merge 1:1 idpers using $datadest/temp_polmodule`y', nogen
keep if status==0
save $datadest/tempp`y', replace
   }

*append the waves
use $datadest/tempp19
foreach y in 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 99 {
      append using $datadest/tempp`y'
   }
   
save $datadest/longfile_child, replace
xtset idpers year
save $datadest/longfile_child, replace

*add social origin data
use $datadest/longfile_child
merge m:1 idpers using shp_so, keepusing(p__o13 p__o14 p__o30 p__o31 p__p47 p__p46 p__o58 p__o59 p__o34 p__o17 is4faj__ is4moj__ tr1faj__ tr1moj__ gldfaj__ gldmoj__) keep (master match)  nogen
save $datadest/longfile_child_so, replace

*add biography data 
use $datadest/longfile_child_so
merge m:1 idpers using shp0_bvfe_user, keepusing(bvfe003 bvfe004) keep (master match) nogen
save $datadest/longfile_child_so_bio, replace


*add master personal file variables
use $datadest/longfile_child_so_bio
merge m:1 idpers using shp_mp, keepusing(sex birthy idfath__ idmoth__) keep (master match) nogen
save $datadest/longfile_child_so_bio_mp, replace

xtset idpers year
save $datadest/SHP_workfile, replace


*syntax Daniel Oesch // adapted to using only ISCO codes

*---------------------------------------------------------------------------------------------
* 1. Create a long file
*---------------------------------------------------------------------------------------------

// create a long file: year variable,

*-------1.1 Create individual files for long file: temporary files for each year
// remove year identifier from variable name, add variable year
// renvars command has to be downloaded from internet if not installed yet: type search renvars, net

local year=1999 // counter for time variable
foreach y in 99 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 { // loop for different years

use idpers idspou?? idhous?? status?? noga2m?? ///
p??w31 is4maj?? xis4ma?? educat?? x??w03 using shp`y'_p_user, clear // open user data
// remove year indication from variable names
renvars idspou?? idhous?? status??  noga2m?? xis4ma?? is4maj?? educat??, postdrop(2)
renpfix p`y' p
renpfix x`y' x
gen year=`year' // create time variable
local year= `year'+1 // change counter for time variable
save $datadest/temp/temp`y', replace // indicate path where you want to store the file
}

*------- 1.2 Append individual files: create long file

use $datadest/temp/temp99
foreach y in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 { // loop for different years
append using $datadest/temp/temp`y' // add cases for each year to the file
tab year // check
}

*----------1.3 Merge file "last job"

merge m:1 idpers using shp_lj, keepusing (is4laj__ xis4la__ p__w114  x__w08  ljyy noga2l__ gldlaj__) ///
keep (match master) 
drop _merge

save $datadest/temp/workfile1, replace // save temporary file

*----------------------------------------
* 2. Create variables used to construct class variable
*-------------------------------

use $datadest/temp/workfile1, clear

*----------- 2.1 Isco: Complete missing information on occupation from proxy-interview (from xis4 to is4)

// variable isco_mainjob: isco code of current year
gen isco_mainjob=is4maj
replace isco_mainjob=xis4ma if (is4maj>=. | is4maj<0)
recode isco_mainjob -9/-1=. // all missing values defined as .

// variable isco_mainjob: isco code of last job

gen isco_lastjob=is4laj__
replace isco_lastjob=xis4la__ if (is4laj__>=. | is4laj__<0)
recode isco_lastjob -9/-1=. // all missing values defined as .

*------------ 2.2 Self employment: complete missing information from proxy-interview (from x$$w03 to pw31)

recode pw31   (-8/-1=1)(1=2)(2/3=3)(4/7=4), gen (selfem_mainjob)
// replace with information from proxy
replace selfem_mainjob=2 if xw03==1 
replace selfem_mainjob=3 if xw03==2 | xw03==3
replace selfem_mainjob=4 if xw03>3 & xw03<8 

label define selfem ///
1 "Not self-employed" ///
2 "Self-empl without employees" ///
3 "Self-empl with 1-9 employees" ///
4 "Self-empl with 10 or more"
label values selfem_mainjob selfem
tab selfem_mainjob

tab p__w114, miss
recode p__w114  (-8=.) (-3/-1=1)(1=2)(2/3=3)(4/7=4), gen (selfem_lastjob)
tab selfem_lastjob
// replace with information from proxy
replace selfem_lastjob=2 if x__w08==1 
replace selfem_lastjob=3 if x__w08==2 | x__w08==3
replace selfem_lastjob=4 if x__w08>3 & x__w08<8 
label values selfem_lastjob selfem
tab selfem_lastjob, miss

*-------------2.3 Sector employment 

gen noga_mainjob=noga2m
tab noga_mainjob
gen noga_lastjob =  noga2l__ 
tab noga_lastjob

*-------------2.4 Recode education into three different levels

recode educat(-7/3=1)(4/6=2)(7/10=3), gen(educ_level)
label define educ_level ///
1 "Less than upper secondary education" ///
2 "Upper secondary education" ///
3 "Tertiary education"
label values educ_level educ_level
tab educ_level


list year isco* selfem* p__w114 x__w08  if idpers==24505102 , nol

*---------------------------------------
* 3. Construct class variable: Recode to the 16-class schema
*--------------------------------------------

// create class variable separately for current year and for last job
// we run the same syntax 1) for the actual year 2) for last job

capture drop class16_mainjob
capture drop class16_lastjob
foreach type in _mainjob _lastjob { 

gen class16`type'=.

* 3.1 Allocate self-employed to schema

replace class16`type'=1 if selfem`type'==4
replace class16`type'=2 if inlist(selfem`type',2,3) & inrange(isco`type',2000,2470)
replace class16`type'=3 if selfem`type'==3 & (inrange(isco`type',1000,1999) | inrange(isco`type',3000,9330))
replace class16`type'=4 if selfem`type'==2 & (inrange(isco`type',1000,1999) | inrange(isco`type',3000,9330))

* 3.2 Allocate employed managers and professionals (ISCO 1000-2470) to the schema

replace class16`type'=5 if selfem`type'==1 & inrange(isco`type',110, 1239)
replace class16`type'=6 if selfem`type'==1 & inrange(isco`type',1300, 1320)
replace class16`type'=9 if selfem`type'==1 & inrange(isco`type',2100, 2213)
replace class16`type'=13 if selfem`type'==1 & inrange(isco`type',2220, 2321)
replace class16`type'=14 if selfem`type'==1 & inrange(isco`type',2330, 2332)
replace class16`type'=13 if selfem`type'==1 & inrange(isco`type',2340, 2359)
replace class16`type'=5 if selfem`type'==1 & inrange(isco`type',2410, 2429)
replace class16`type'=14 if selfem`type'==1 & inrange(isco`type',2430, 2432)
replace class16`type'=13 if selfem`type'==1 & isco`type'==2440
replace class16`type'=5 if selfem`type'==1 & isco`type'==2441
replace class16`type'=13 if selfem`type'==1 & inrange(isco`type',2442, 2443)
replace class16`type'=14 if selfem`type'==1 & isco`type'==2444
replace class16`type'=13 if selfem`type'==1 & isco`type'==2445
replace class16`type'=14 if selfem`type'==1 & inrange(isco`type',2446, 2450)
replace class16`type'=13 if selfem`type'==1 & isco`type'==2451
replace class16`type'=14 if selfem`type'==1 & inrange(isco`type',2452, 2455)
replace class16`type'=13 if selfem`type'==1 & isco`type'==2460
replace class16`type'=5 if selfem`type'==1 & isco`type'==2470

* 3.3 Allocate employed associate managers and semi-professionals (ISCO 3000) to the schema

replace class16`type'=10 if selfem`type'==1 & inrange(isco`type',3100, 3152)
replace class16`type'=14 if selfem`type'==1 & isco`type'==3200
replace class16`type'=10 if selfem`type'==1 & inrange(isco`type',3210, 3213)
replace class16`type'=14 if selfem`type'==1 & inrange(isco`type',3220, 3224)
replace class16`type'=15 if selfem`type'==1 & isco`type'==3225
replace class16`type'=14 if selfem`type'==1 & isco`type'==3226
replace class16`type'=15 if selfem`type'==1 & inrange(isco`type',3227, 3228)
replace class16`type'=14 if selfem`type'==1 & inrange(isco`type',3229, 3340)
replace class16`type'=6 if selfem`type'==1 & inrange(isco`type',3400, 3433)
replace class16`type'=10 if selfem`type'==1 & isco`type'==3434
replace class16`type'=6 if selfem`type'==1 & inrange(isco`type',3440, 3450)
replace class16`type'=14 if selfem`type'==1 & inrange(isco`type',3460, 3472)
replace class16`type'=15 if selfem`type'==1 & inrange(isco`type',3473, 3475)
replace class16`type'=14 if selfem`type'==1 & isco`type'==3480


* 3.4 Allocate specific occupations to the schema

*Professional military forces (isco 100) to associate managers 
replace class16`type'=6 if selfem`type'==1 & isco`type'==100

*Locomotive engine drivers to technicians
replace class16`type'=10 if selfem`type'==1 & inlist(isco`type',8310,8311)

*Police officers to associate managers
replace class16`type'=6 if selfem`type'==1 & isco`type'==5162

*Bus and tram drivers to skilled and routine service workers
replace class16`type'=15 if selfem`type'==1 & isco`type'==8323

* 3.5 Allocate intermediate occupations to the schema (ISCO 4000-8340) and correct for skilled/unskilled (with/without upper secondary education)

replace class16`type'=7 if selfem`type'==1 & educ_level!=1 & inrange(isco`type',4000, 4223)
replace class16`type'=8 if selfem`type'==1 & educ_level==1 & inrange(isco`type',4000, 4223)
replace class16`type'=15 if selfem`type'==1 & educ_level!=1 & inlist(isco`type',5160, 5169)
replace class16`type'=16 if selfem`type'==1 & educ_level==1 & inlist(isco`type',5160, 5169)
replace class16`type'=7 if selfem`type'==1 & inlist(isco`type',5161, 5163)

replace class16`type'=15 if selfem`type'==1 & educ_level!=1 & inrange(isco`type',5000, 5132)
replace class16`type'=16 if selfem`type'==1 & educ_level==1 & inrange(isco`type',5000, 5132)
replace class16`type'=16 if selfem`type'==1 & inlist(isco`type',5133, 5139)
replace class16`type'=15 if selfem`type'==1 & educ_level!=1 & inlist(isco`type',5140, 5141, 5143)
replace class16`type'=16 if selfem`type'==1 & educ_level==1 & inlist(isco`type',5140, 5141, 5143)
replace class16`type'=16 if selfem`type'==1 & inlist(isco`type',5142, 5149)
replace class16`type'=15 if selfem`type'==1 & educ_level!=1 & inrange(isco`type',5200, 5220)
replace class16`type'=16 if selfem`type'==1 & educ_level==1 & inrange(isco`type',5200, 5220)

replace class16`type'=11 if selfem`type'==1 & educ_level!=1 & inrange(isco`type',6000, 6154)
replace class16`type'=12 if selfem`type'==1 & educ_level==1 & inrange(isco`type',6000, 6154)

replace class16`type'=11 if selfem`type'==1 & educ_level!=1 & inrange(isco`type',7000, 7442)
replace class16`type'=12 if selfem`type'==1 & educ_level==1 & inrange(isco`type',7000, 7442)

replace class16`type'=11 if selfem`type'==1 & educ_level!=1 & inrange(isco`type',8000, 8279)
replace class16`type'=12 if selfem`type'==1 & educ_level==1 & inrange(isco`type',8000, 8279)
replace class16`type'=12 if selfem`type'==1 & inrange(isco`type',8280, 8300)
replace class16`type'=11 if selfem`type'==1 & educ_level!=1 & isco`type'==8312
replace class16`type'=12 if selfem`type'==1 & educ_level==1 & isco`type'==8312
replace class16`type'=12 if selfem`type'==1 & inrange(isco`type',8320, 8322)
replace class16`type'=11 if selfem`type'==1 & educ_level!=1 & isco`type'==8324
replace class16`type'=12 if selfem`type'==1 & educ_level==1 & isco`type'==8324

replace class16`type'=12 if selfem`type'==1 & isco`type'==8330
replace class16`type'=11 if selfem`type'==1 & educ_level!=1 & inrange(isco`type',8331, 8340)
replace class16`type'=12 if selfem`type'==1 & educ_level==1 & inrange(isco`type',8331, 8340)

* 3.6 Allocate elementary occupations (ISCO 9000-9330 )

replace class16`type'=12 if selfem`type'==1 & inrange(isco`type',9100, 9152)
replace class16`type'=16 if selfem`type'==1 & inrange(isco`type',9153, 9330)

replace class16`type'=12 if selfem`type'==1 & isco`type'==9000 & inrange(noga`type',1,6)
replace class16`type'=12 if selfem`type'==1 & isco`type'==9000 & (inlist(noga`type',-3,-2) | inrange(noga`type',7,17))

* 3.7 Occupations impossible to allocate to a given class

replace class16`type'=. if isco`type'==2000
replace class16`type'=. if isco`type'==3000

}

tab class16_mainjob, miss
tab class16_lastjob, miss


*-------------------------------------
* 4. Replace class with previous waves and last job 
*-------------------------------------

*--------------4.1 define the panel
xtset idpers year

*--------------4.2 Create class variable as class of current year 

gen class16 = class16_mainjob
count if class16==.


label define class16 ///
1 "Large employers (10 or more employees)" ///
2 "Self-employed professionals" ///
3 "Small business owners with 1-9 employees" ///
4 "Small business owners without employees" ///
5 "Managers and administrators" ///
6 "Associate managers and administrators" ///
7 "Skilled clerks" ///
8 "Routine clerks" ///
9 "Technical experts" ///
10 "Technicians" ///
11 "Skilled craft workers" ///
12 "Routine operatives" ///
13 "Socio-cultural professionals" ///
14 "Socio-cultural semi-professionals" ///
15 "Skilled service workers" ///
16 "Routine service workers"
label val class16 class16

*--------------4.3 If class16 is missing: replace with class of previous waves
forval l = 1/19 {
replace class16=l`l'.class16_mainjob if class16>=.
count if class16==.
}

*--------------4.4 if class16 is missing: replace with class from last job

replace class16=class16_lastjob if class16==.
count if class16==.

save $datadest/SHP_workfile, replace

*final working file
set more off

use $datadest/SHP_workfile
cd "DEFINE"

keep if status==0
numlabel, add

*select variables to keep and save as new dataset
keep year idpers idhous status age relarp idspou civsta educat occupa nat_1_ nat_2_ nat_3_ plingu pw31 is4maj pp01 pp10 pp19 pr04 pp50 pp51 pp52 is4faj__ p__o13 p__o14 p__o17 is4moj__ p__o30 p__o31 p__o34 birthy sex educ_level class16_mainjob class16_lastjob class16 class16_p class16p gldfaj__ gldmoj__ iptotg gldmaj gldlaj__


*recode parental class Oesch

*1. father's class
*---rename and recode----

rename is4faj__ visco
rename p__o13 vempl
rename p__o14 vempl_nr


tab1 vempl vempl_nr

recode vempl vempl_nr (-3=.c) (-2=.b) (-1=.a)

gen class16_v=.

* Large employers (1)
// more than 9 employees

replace class16_v=1 if vempl_nr>=4 & vempl_nr<.


* Self-employed professionals (2)
// no employees

replace class16_v=2 if (vempl==3 | vempl==4) & (vempl_nr==1) & (visco >= 2000 & visco <= 2229) 
replace class16_v=2 if (vempl==3 | vempl==4) & (vempl_nr==1) & (visco >= 2300 & visco <= 2470)

replace class16_v=2 if (vempl==3 | vempl==4) & (vempl_nr==1) & (visco >= 2000 & visco <= 2229) 
replace class16_v=2 if (vempl==3 | vempl==4) & (vempl_nr==1) & (visco >= 2300 & visco <= 2470)


* Small business owners with employees (3)
// less than 9 employees

replace class16_v=3 if (vempl==3 | vempl==4) & (vempl_nr==2 | vempl_nr==3) & (visco >= 1000 & visco <= 1999)
replace class16_v=3 if (vempl==3 | vempl==4) & (vempl_nr==2 | vempl_nr==3) & (visco >= 3000 & visco <= 9333)
replace class16_v=3 if (vempl==3 | vempl==4) & (vempl_nr==2 | vempl_nr==3) & (visco == 2230)

* Small business owners without employees (4)

replace class16_v=4 if (vempl==3 | vempl==4) & (vempl_nr==1) & (visco >= 1000 & visco <= 1999)
replace class16_v=4 if (vempl==3 | vempl==4) & (vempl_nr==1) & (visco >= 3000 & visco <= 9333)
replace class16_v=4 if (vempl==3 | vempl==4) & (vempl_nr==1) & (visco == 2230)

* Technical experts (5)

replace class16_v=5 if (vempl!=3 & vempl!=4) & (visco >= 2100 & visco <= 2213)

* Technicians (6)

replace class16_v=6 if (vempl!=3 & vempl!=4) & (visco >= 3100 & visco <= 3152)
replace class16_v=6 if (vempl!=3 & vempl!=4) & (visco >= 3210 & visco <= 3213)
replace class16_v=6 if (vempl!=3 & vempl!=4) & (visco == 3434)

* Skilled craft and production workers (7)

replace class16_v=7 if (vempl!=3 & vempl!=4) & (visco >= 6000 & visco <= 7442)
replace class16_v=7 if (vempl!=3 & vempl!=4) & (visco >= 8310 & visco <= 8312)
replace class16_v=7 if (vempl!=3 & vempl!=4) & (visco >= 8324 & visco <= 8330)
replace class16_v=7 if (vempl!=3 & vempl!=4) & (visco >= 8332 & visco <= 8340)

* Low-skilled production workers (8)

replace class16_v=8 if (vempl!=3 & vempl!=4) & (visco >= 8000 & visco <= 8300)
replace class16_v=8 if (vempl!=3 & vempl!=4) & (visco >= 8320 & visco <= 8321)
replace class16_v=8 if (vempl!=3 & vempl!=4) & (visco == 8331)
replace class16_v=8 if (vempl!=3 & vempl!=4) & (visco >= 9153 & visco <= 9333)

* Higher-grade managers and administrators (9)

replace class16_v=9 if (vempl!=3 & vempl!=4) & (visco >= 1000 & visco <= 1239)
replace class16_v=9 if (vempl!=3 & vempl!=4) & (visco >= 2400 & visco <= 2429)
replace class16_v=9 if (vempl!=3 & vempl!=4) & (visco == 2441)
replace class16_v=9 if (vempl!=3 & vempl!=4) & (visco == 2470)

* Lower-grade managers and administrators (10)

replace class16_v=10 if (vempl!=3 & vempl!=4) & (visco >= 1300 & visco <= 1319)
replace class16_v=10 if (vempl!=3 & vempl!=4) & (visco >= 3400 & visco <= 3433)
replace class16_v=10 if (vempl!=3 & vempl!=4) & (visco >= 3440 & visco <= 3450)

* Skilled clerks (11)

replace class16_v=11 if (vempl!=3 & vempl!=4) & (visco >= 4000 & visco <= 4112)
replace class16_v=11 if (vempl!=3 & vempl!=4) & (visco >= 4114 & visco <= 4210)
replace class16_v=11 if (vempl!=3 & vempl!=4) & (visco >= 4212 & visco <= 4222)

* Low-skilled clerks (12)

replace class16_v=12 if (vempl!=3 & vempl!=4) & (visco == 4113)
replace class16_v=12 if (vempl!=3 & vempl!=4) & (visco == 4211)
replace class16_v=12 if (vempl!=3 & vempl!=4) & (visco == 4223)

* Socio-cultural professionals (13)

replace class16_v=13 if (vempl!=3 & vempl!=4) & (visco >= 2220 &  visco <= 2229)
replace class16_v=13 if (vempl!=3 & vempl!=4) & (visco >= 2300 &  visco <= 2320)
replace class16_v=13 if (vempl!=3 & vempl!=4) & (visco >= 2340 &  visco <= 2359)
replace class16_v=13 if (vempl!=3 & vempl!=4) & (visco >= 2430 &  visco <= 2440)
replace class16_v=13 if (vempl!=3 & vempl!=4) & (visco >= 2442 &  visco <= 2443)
replace class16_v=13 if (vempl!=3 & vempl!=4) & (visco == 2445)
replace class16_v=13 if (vempl!=3 & vempl!=4) & (visco == 2451)
replace class16_v=13 if (vempl!=3 & vempl!=4) & (visco == 2460)

* Socio-cultural semi-professionals (14)

replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco == 2230)
replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco >= 2330 & visco <= 2332)
replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco == 2444)
replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco >= 2446 & visco <= 2450)
replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco >= 2452 & visco <= 2455)
replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco == 3200)
replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco >= 3220 & visco <= 3224)
replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco == 3226)
replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco >= 3229 & visco <= 3340)
replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco >= 3460 & visco <= 3472)
replace class16_v=14 if (vempl!=3 & vempl!=4) & (visco == 3480)

* Skilled service (15)

replace class16_v=15 if (vempl!=3 & vempl!=4) & (visco == 3225)
replace class16_v=15 if (vempl!=3 & vempl!=4) & (visco >= 3227 & visco <= 3228)
replace class16_v=15 if (vempl!=3 & vempl!=4) & (visco >= 3473 & visco <= 3475)
replace class16_v=15 if (vempl!=3 & vempl!=4) & (visco >= 5000 & visco <= 5113)
replace class16_v=15 if (vempl!=3 & vempl!=4) & (visco == 5122)
replace class16_v=15 if (vempl!=3 & vempl!=4) & (visco >= 5131 & visco <= 5132)
replace class16_v=15 if (vempl!=3 & vempl!=4) & (visco >= 5140 & visco <= 5141)
replace class16_v=15 if (vempl!=3 & vempl!=4) & (visco == 5143)
replace class16_v=15 if (vempl!=3 & vempl!=4) & (visco >= 5160 & visco <= 5220)
replace class16_v=15 if (vempl!=3 & vempl!=4) & (visco == 8323)

* Low-skilled service (16)

replace class16_v=16 if (vempl!=3 & vempl!=4) & (visco >= 5120 & visco <= 5121)
replace class16_v=16 if (vempl!=3 & vempl!=4) & (visco >= 5123 & visco <= 5130)
replace class16_v=16 if (vempl!=3 & vempl!=4) & (visco >= 5133 & visco <= 5139)
replace class16_v=16 if (vempl!=3 & vempl!=4) & (visco == 5142)
replace class16_v=16 if (vempl!=3 & vempl!=4) & (visco == 5149)
replace class16_v=16 if (vempl!=3 & vempl!=4) & (visco == 5230)
replace class16_v=16 if (vempl!=3 & vempl!=4) & (visco == 8322)
replace class16_v=16 if (vempl!=3 & vempl!=4) & (visco >= 9100 &  visco <= 9152)


label variable class16_v "Father's Oesch class position - 16 classes"
label define class16_v ///
1 "Large employers" ///
2 "Self-employed professionals" ///
3 "Small business owners with employees" ///
4 "Small business owners without employees" ///
5 "Technical experts" ///
6 "Technicians" ///
7 "Skilled craft and production workers" ///
8 "Low-skilled craft and production workers" ///
9 "Higher-grade managers and administrators" ///
10 "Lower-grade managers and administrators" ///
11 "Skilled clerks" ///
12 "Low-skilled clerks" ///
13 "Socio-cultural professionals" ///
14 "Socio-cultural semi-professionals" ///
15 "Skilled service" ///
16 "Low-skilled service"
label value class16_v class16_v
tab class16_v


*recoding into 8 classes
recode class16_v (1 2=1)(3 4=2)(5 6=3)(7 8=4)(9 10=5)(11 12=6)(13 14=7)(15 16=8), gen(class8_v)
label variable class8_v "Father's Oesch class position - 8 classes"

label define class8_v ///
1 "Self-employed professionals and large employers" ///
2 "Small business owners" ///
3 "Technical (semi-)professionals" ///
4 "Production workers" ///
5 "(Associate) managers" ///
6 "Clerks" ///
7 "Socio-cultural (semi-)professionals" ///
8 "Service workers"
label value class8_v class8_v
tab class8_v

*2. mother's class
*---rename and recode----

rename is4moj__ misco
rename p__o30 mempl
rename p__o31 mempl_nr


tab1 mempl mempl_nr

recode mempl mempl_nr (-3=.c) (-2=.b) (-1=.a)

gen class16_m=.

* Large employers (1)
// more than 9 employees

replace class16_m=1 if mempl_nr>=4 & mempl_nr<.


* Self-employed professionals (2)
// no employees

replace class16_m=2 if (mempl==3 | mempl==4) & (mempl_nr==1) & (misco >= 2000 & misco <= 2229) 
replace class16_m=2 if (mempl==3 | mempl==4) & (mempl_nr==1) & (misco >= 2300 & misco <= 2470)

replace class16_m=2 if (mempl==3 | mempl==4) & (mempl_nr==1) & (misco >= 2000 & misco <= 2229) 
replace class16_m=2 if (mempl==3 | mempl==4) & (mempl_nr==1) & (misco >= 2300 & misco <= 2470)


* Small business owners with employees (3)
// less than 9 employees

replace class16_m=3 if (mempl==3 | mempl==4) & (mempl_nr==2 | mempl_nr==3) & (misco >= 1000 & misco <= 1999)
replace class16_m=3 if (mempl==3 | mempl==4) & (mempl_nr==2 | mempl_nr==3) & (misco >= 3000 & misco <= 9333)
replace class16_m=3 if (mempl==3 | mempl==4) & (mempl_nr==2 | mempl_nr==3) & (misco == 2230)

* Small business owners without employees (4)

replace class16_m=4 if (mempl==3 | mempl==4) & (mempl_nr==1) & (misco >= 1000 & misco <= 1999)
replace class16_m=4 if (mempl==3 | mempl==4) & (mempl_nr==1) & (misco >= 3000 & misco <= 9333)
replace class16_m=4 if (mempl==3 | mempl==4) & (mempl_nr==1) & (misco == 2230)

* Technical experts (5)

replace class16_m=5 if (mempl!=3 & mempl!=4) & (misco >= 2100 & misco <= 2213)

* Technicians (6)

replace class16_m=6 if (mempl!=3 & mempl!=4) & (misco >= 3100 & misco <= 3152)
replace class16_m=6 if (mempl!=3 & mempl!=4) & (misco >= 3210 & misco <= 3213)
replace class16_m=6 if (mempl!=3 & mempl!=4) & (misco == 3434)

* Skilled craft and production workers (7)

replace class16_m=7 if (mempl!=3 & mempl!=4) & (misco >= 6000 & misco <= 7442)
replace class16_m=7 if (mempl!=3 & mempl!=4) & (misco >= 8310 & misco <= 8312)
replace class16_m=7 if (mempl!=3 & mempl!=4) & (misco >= 8324 & misco <= 8330)
replace class16_m=7 if (mempl!=3 & mempl!=4) & (misco >= 8332 & misco <= 8340)

* Low-skilled production workers (8)

replace class16_m=8 if (mempl!=3 & mempl!=4) & (misco >= 8000 & misco <= 8300)
replace class16_m=8 if (mempl!=3 & mempl!=4) & (misco >= 8320 & misco <= 8321)
replace class16_m=8 if (mempl!=3 & mempl!=4) & (misco == 8331)
replace class16_m=8 if (mempl!=3 & mempl!=4) & (misco >= 9153 & misco <= 9333)

* Higher-grade managers and administrators (9)

replace class16_m=9 if (mempl!=3 & mempl!=4) & (misco >= 1000 & misco <= 1239)
replace class16_m=9 if (mempl!=3 & mempl!=4) & (misco >= 2400 & misco <= 2429)
replace class16_m=9 if (mempl!=3 & mempl!=4) & (misco == 2441)
replace class16_m=9 if (mempl!=3 & mempl!=4) & (misco == 2470)

* Lower-grade managers and administrators (10)

replace class16_m=10 if (mempl!=3 & mempl!=4) & (misco >= 1300 & misco <= 1319)
replace class16_m=10 if (mempl!=3 & mempl!=4) & (misco >= 3400 & misco <= 3433)
replace class16_m=10 if (mempl!=3 & mempl!=4) & (misco >= 3440 & misco <= 3450)

* Skilled clerks (11)

replace class16_m=11 if (mempl!=3 & mempl!=4) & (misco >= 4000 & misco <= 4112)
replace class16_m=11 if (mempl!=3 & mempl!=4) & (misco >= 4114 & misco <= 4210)
replace class16_m=11 if (mempl!=3 & mempl!=4) & (misco >= 4212 & misco <= 4222)

* Low-skilled clerks (12)

replace class16_m=12 if (mempl!=3 & mempl!=4) & (misco == 4113)
replace class16_m=12 if (mempl!=3 & mempl!=4) & (misco == 4211)
replace class16_m=12 if (mempl!=3 & mempl!=4) & (misco == 4223)

* Socio-cultural professionals (13)

replace class16_m=13 if (mempl!=3 & mempl!=4) & (misco >= 2220 &  misco <= 2229)
replace class16_m=13 if (mempl!=3 & mempl!=4) & (misco >= 2300 &  misco <= 2320)
replace class16_m=13 if (mempl!=3 & mempl!=4) & (misco >= 2340 &  misco <= 2359)
replace class16_m=13 if (mempl!=3 & mempl!=4) & (misco >= 2430 &  misco <= 2440)
replace class16_m=13 if (mempl!=3 & mempl!=4) & (misco >= 2442 &  misco <= 2443)
replace class16_m=13 if (mempl!=3 & mempl!=4) & (misco == 2445)
replace class16_m=13 if (mempl!=3 & mempl!=4) & (misco == 2451)
replace class16_m=13 if (mempl!=3 & mempl!=4) & (misco == 2460)

* Socio-cultural semi-professionals (14)

replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco == 2230)
replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco >= 2330 & misco <= 2332)
replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco == 2444)
replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco >= 2446 & misco <= 2450)
replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco >= 2452 & misco <= 2455)
replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco == 3200)
replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco >= 3220 & misco <= 3224)
replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco == 3226)
replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco >= 3229 & misco <= 3340)
replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco >= 3460 & misco <= 3472)
replace class16_m=14 if (mempl!=3 & mempl!=4) & (misco == 3480)

* Skilled service (15)

replace class16_m=15 if (mempl!=3 & mempl!=4) & (misco == 3225)
replace class16_m=15 if (mempl!=3 & mempl!=4) & (misco >= 3227 & misco <= 3228)
replace class16_m=15 if (mempl!=3 & mempl!=4) & (misco >= 3473 & misco <= 3475)
replace class16_m=15 if (mempl!=3 & mempl!=4) & (misco >= 5000 & misco <= 5113)
replace class16_m=15 if (mempl!=3 & mempl!=4) & (misco == 5122)
replace class16_m=15 if (mempl!=3 & mempl!=4) & (misco >= 5131 & misco <= 5132)
replace class16_m=15 if (mempl!=3 & mempl!=4) & (misco >= 5140 & misco <= 5141)
replace class16_m=15 if (mempl!=3 & mempl!=4) & (misco == 5143)
replace class16_m=15 if (mempl!=3 & mempl!=4) & (misco >= 5160 & misco <= 5220)
replace class16_m=15 if (mempl!=3 & mempl!=4) & (misco == 8323)

* Low-skilled service (16)

replace class16_m=16 if (mempl!=3 & mempl!=4) & (misco >= 5120 & misco <= 5121)
replace class16_m=16 if (mempl!=3 & mempl!=4) & (misco >= 5123 & misco <= 5130)
replace class16_m=16 if (mempl!=3 & mempl!=4) & (misco >= 5133 & misco <= 5139)
replace class16_m=16 if (mempl!=3 & mempl!=4) & (misco == 5142)
replace class16_m=16 if (mempl!=3 & mempl!=4) & (misco == 5149)
replace class16_m=16 if (mempl!=3 & mempl!=4) & (misco == 5230)
replace class16_m=16 if (mempl!=3 & mempl!=4) & (misco == 8322)
replace class16_m=16 if (mempl!=3 & mempl!=4) & (misco >= 9100 &  misco <= 9152)


label variable class16_m "Mother's Oesch class position - 16 classes"
label define class16_m ///
1 "Large employers" ///
2 "Self-employed professionals" ///
3 "Small business owners with employees" ///
4 "Small business owners without employees" ///
5 "Technical experts" ///
6 "Technicians" ///
7 "Skilled craft and production workers" ///
8 "Low-skilled craft and production workers" ///
9 "Higher-grade managers and administrators" ///
10 "Lower-grade managers and administrators" ///
11 "Skilled clerks" ///
12 "Low-skilled clerks" ///
13 "Socio-cultural professionals" ///
14 "Socio-cultural semi-professionals" ///
15 "Skilled service" ///
16 "Low-skilled service"
label value class16_m class16_m
tab class16_m


*recoding into 8 classes
recode class16_m (1 2=1)(3 4=2)(5 6=3)(7 8=4)(9 10=5)(11 12=6)(13 14=7)(15 16=8), gen(class8_m)
label variable class8_m "Mother's Oesch class position - 8 classes"

label define class8_m ///
1 "Self-employed professionals and large employers" ///
2 "Small business owners" ///
3 "Technical (semi-)professionals" ///
4 "Production workers" ///
5 "(Associate) managers" ///
6 "Clerks" ///
7 "Socio-cultural (semi-)professionals" ///
8 "Service workers"
label value class8_m class8_m
tab class8_m

***********************
*combine into parental class

tab1 class8_v class8_m

capture drop class8_parent
gen class8_parent = class8_v
replace class8_parent = class8_m if class8_v==.
label values class8_parent class8_v

tab1 class8_v class8_m class8_parent

***********************
*respondent's class16

*recode 16-class Oesch into 8-class (compatible with G-SOEP)
*recode SHP version of Oesch scheme --> in line with G-SOEP (father's class is based on that)
recode class16 5=9 6=10 7=11 8=12 9=5 10=6 11=7 12=8, gen(class16_G)
label variable class16_G "Respondent's GSOEP config Oesch class position - 16 classes"
label values class16_G class16_v
tab1 class16_G class16_v

*recode into 8 classes
recode class16_G (1 2=1)(3 4=2)(5 6=3)(7 8=4)(9 10=5)(11 12=6)(13 14=7)(15 16=8), gen(class8_r)
label values class8_r class8_v
label variable class8_r "Respondent's GSOEP config Oesch class position - 8 classes"
tab class8_r

************************
*recode transitions using this variable

* Simplified measure of class
recode class8_parent (3=1) (5=1) (6=9) (2=9) , gen (simclass_parent)
recode simclass_parent (4=2) (7=3) (8=4) (9=5)
label define simclass 1"Old middle class" 2"Production wrkrs" 3"SCP" 4"Service wrkrs" 5"Others"
label values simclass_parent simclass

recode class8_r (3=1) (5=1) (6=9) (2=9) , gen (simclass_r)
recode simclass_r (4=2) (7=3) (8=4) (9=5)
label values simclass_r simclass

*Different order of variables
capture drop simclass_r2
recode simclass_r simclass_parent (1=4) (2=2) (3=3) (4=1), gen(simclass_r2 simclass_parent2)
label define simclass_r2 1"Service workers" 2"Production workers" 3 "SCP" 4"OMC" 5"Others"
label values simclass_r2 simclass_parent2 simclass_r2
tab1 simclass_r2 simclass_r 
tab1 simclass_parent2 simclass_parent

*recoding of transitions

gen trans=.
recode trans (.=1) if simclass_parent==2 & simclass_r==2 // * Immobile prod workers
recode trans (.=2) if simclass_parent==4 & simclass_r==4 //   Immobile service workers
recode trans (.=3) if simclass_parent==2 & simclass_r==4 // * Prod wkers - Serv wkers
recode trans (.=4) if simclass_parent==4 & simclass_r==2 //   Serv wkers - Prod wkers
recode trans (.=5) if simclass_parent==3 & simclass_r==3 // * Immobile SCP

recode trans (.=6) if simclass_parent==2 & simclass_r==3 // * Prod wkers - SCP
recode trans (.=7) if simclass_parent==4 & simclass_r==3 //   Serv wkers - SCP
recode trans (.=8) if simclass_parent==3 & simclass_r==2 //   SCP - worker
recode trans (.=8) if simclass_parent==3 & simclass_r==4 //   SCP - worker

recode trans (.=9) if simclass_parent==2 & simclass_r==1 // * Prod wkers - OMC
recode trans (.=10) if simclass_parent==4 & simclass_r==1 //  Serv wkers - OMC
recode trans (.=11) if simclass_parent==3 & simclass_r==1 // * SCP - OMC

recode trans (.=12) if simclass_parent==1 & simclass_r==2 //  OMC - workers
recode trans (.=12) if simclass_parent==1 & simclass_r==4 //  OMC - workers
recode trans (.=13) if simclass_parent==1 & simclass_r==3 // * OMC - SCP
recode trans (.=14) if simclass_parent==1 & simclass_r==1 // * Immobile OMC

recode trans (.=20) if simclass_parent==1 & simclass_r==5
recode trans (.=21) if simclass_parent==2 & simclass_r==5
recode trans (.=22) if simclass_parent==3 & simclass_r==5
recode trans (.=23) if simclass_parent==4 & simclass_r==5
recode trans (.=24) if simclass_parent==5 & simclass_r==1
recode trans (.=25) if simclass_parent==5 & simclass_r==2
recode trans (.=26) if simclass_parent==5 & simclass_r==3
recode trans (.=27) if simclass_parent==5 & simclass_r==4
recode trans (.=28) if simclass_parent==5 & simclass_r==5

label define trans ///
1 "Immobile prod workers" ///
2 "Immobile serv workers" ///
3 "Prod wkers - Serv wkers" ///
4 "Serv wkers - Prod wkers" ///
5 "Immobile SCP" ///
6 "Prod wkers - SCP" ///
7 "Serv wkers - SCP" ///
8 "SCP - Workers" ///
9 "Prod wkers - OMC" ///
10 "Serv wkers - OMC" ///
11 "SCP - OMC" ///
12 "OMC - Workers" ///
13 "OMC - SCP" ///
14 "Immobile OMC"
label values trans trans

*basic variable recoding

*recode civstatus 
recode civsta (1=3 "other") (2=1 "married/partnership") (3=2 "divorced/separated") (4=2) (5=3) (6=1) (7=3), gen(civsta_3)
tab civsta_3

*full-part-time
recode occupa (1=0 "full-time") (2=1 "part-time") (else=.), gen(parttime)
tab parttime

*gender
recode sex(2=1 "female") (1=0 "male"), gen(female)
tab female

*level of education
recode educat (0=0 "still in school") (1/3=1 "low") (4/5=2 "medium") (6/10=3 "high"), gen(educat_3)
tab1 educat educat_3

*party preference (if elections held today)

*Social-democratic party only
tab pp19
recode pp19 (3=1 "SP") (else=0), gen(SP)
tab SP
// 0 category now also includes people who would not vote, vote for candidate not party, or for no party 

recode pp19 (3=1 "SP") (50 51 52=.) (else=0), gen(SP2)
// these categories are now set to missing

*Left party identification/preference excl. Greens (SD; PST = Swiss Labour Party)
recode pp19 (3=1 "SP/PST") (9=1 "SP/PST") (50 51 52=.) (else=0), gen(left)

*Left party identification/preference incl. Greens
recode pp19 (3=1 "SP/PST/Greens") (9=1 "SP/PST/Greens") (11=1 "SP/PST/Greens") (50 51 52=.) (else=0), gen(left_green)

tab1 SP2 left left_green

*nationality
tab1 nat_1_ nat_2_ nat_3_
gen swiss=.
replace swiss=1 if nat_1_==8100 | nat_2_==8100 | nat_3_==8100
replace swiss=0 if (nat_1_!=8100 & nat_1_<.) | (nat_2_!=8100 & nat_2_<.) | (nat_3_!=8100 & nat_3_<.)
tab swiss

* Generations of respondents

tab birthy
recode birthy (1906/1927=1) (1928/1945=2) (1946/1964=3) (1965/1980=4) (1981/1996=5) (1997/2005=6) ///
 , gen(generations)
 
label define generations ///
1 "1927 or earlier" ///
2 "Silent 1928-1945" ///
3 "Boomers 1946-1964" ///
4 "Gen X 1965-1980" ///
5 "Millennials 1981-1996" ///
6 "Gen Z 1997-2012"
label values generations generations
tab generations

recode birthy (1906/1927=.) (1928/1945=.) (1946/1964=3) (1965/1980=4) (1981/1996=5) (1997/2005=.), gen(generations2)
 
label define generations2 ///
3 "Boomers 1946-1964" ///
4 "Gen X 1965-1980" ///
5 "Millennials 1981-1996"
label values generations2 generations2
tab generations2

*income - log of gross yearly income
summarize iptotg, detail
gen lnincome=ln(iptotg)
summarize lnincome, detail 

*EGP class scheme parents
tab1 gldfaj__ gldmoj__,m

recode gldfaj__ (1=1 "higher service") (2=2 "lower service") (3=3 "routine non-manual") (4 5 11 =5 "small self-empl & farmers") (7 8 9 10= 4 "working class"), gen(egp_f)

recode gldmoj__ (1=1 "higher service") (2=2 "lower service") (3=3 "routine non-manual") (4 5 11 =5 "small self-empl & farmers") (7 8 9 10= 4 "working class"), gen(egp_m)

gen egp_parent = egp_f
replace egp_parent = egp_m if egp_f==. | egp_f==.c | egp_f==.d
label values egp_parent egp_f
tab egp_parent

*EGP class scheme respondents
tab1 gldmaj gldlaj__, m
recode gldlaj__ (-4=.d) (-3=.c) // .d not classified .c inapplicable
gen egp_r=gldmaj
replace egp_r=gldlaj__ if egp_r==.c | egp_r==.d
tab egp_r
recode egp_r (1=1) (2=2) (3=3) (4 5 11 =5) (7 8 9 10= 4)
label values egp_r egp_f
tab egp_r

numlabel, add
 
save SHP_workfile_recoded, replace
